"""
给 MIDOG 2021 中  tif 标记出框

https://midog.deepmicroscopy.org/download-dataset/

"""
import json
import os
import numpy as np
from PIL import Image
import cv2
from tqdm import tqdm

if __name__ == '__main__':
    midog = 'MIDOG2021'
    wsi_path = f"/media/hsmy/wanghao_18T/dataset/{midog}/tiff/"
    midog_json = f"/media/hsmy/wanghao_18T/dataset/{midog}/MIDOG.json"
    json_data = json.load(open(midog_json))
    for wsi_file in os.listdir(wsi_path):
        file_name = wsi_file.split('.')[0]
        if int(file_name) != 24:
            continue
        file_path = os.path.join(wsi_path, wsi_file)
        img = cv2.imread(file_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        annotations = [anno for anno in json_data["annotations"] if
                       anno["image_id"] == int(file_name) and anno["category_id"] == 1]

        for index, anno in enumerate(annotations):
            bbox_arr = anno["bbox"]
            print(bbox_arr)
            x1 = int(bbox_arr[0])
            y1 = int(bbox_arr[1])
            x2 = int(bbox_arr[2])
            y2 = int(bbox_arr[3])
            img = cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 5)
            img = cv2.putText(img, f"({x1},{y1})", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2, cv2.LINE_AA)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        cv2.imwrite(f"/media/hsmy/wanghao_18T/dataset/MIDOG2021/tiff_gt/{file_name}.png", img)